package com.ants.modules.douYin;

/**
 * @className: DouYinParse
 * @description: TODO 类描述
 * @author: Mr.ChenShuang
 * @date: 2023/03/06 15:01
 **/

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// https://maven.aliyun.com/repository/public/com/alibaba/fastjson/1.2.76/fastjson-1.2.76.jar
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * TODO     抖音去水印
 *
 * @Date 2023/3/6 16:00
 * @Author chenshuang
 * @Param null
 * @Return {@link null}
 */
@RestController
public class DouYinParse {

    @GetMapping("/douyin")
    public void douyin(HttpServletResponse response, @RequestParam String url) {
//        String regex = "https?://(\\w|-)+(\\.(\\w|-)+)+(/(\\w+(\\?(\\w+=(\\w|%|-)*(\\&\\w+=(\\w|%|-)*)*)?)?)?)+";//匹配网址
        String regex = "(https?|http):\\/\\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";//匹配网址
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(url);
        if (m.find()) {
            String substring = url.substring(m.start(), m.end());
            String resp = HttpRequest.get("https://tenapi.cn/v2/video?url=" + substring).execute().body();
            JSONObject entries = JSONUtil.parseObj(resp);
            Integer code = entries.getInt("code");
            if (code.equals(200)) {
                JSONObject data = entries.getJSONObject("data");
                String respUrl = data.getStr("url");
                System.out.println(respUrl);
                HttpResponse execute = HttpRequest.get(respUrl).execute();
                InputStream inputStream = execute.bodyStream();
                String fileName = UUID.randomUUID() + ".mp4";
                // 其余处理略
                OutputStream outputStream = null;
                try {
                    response.setContentType("application/force-download");// 设置强制下载不打开
                    response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"), "iso-8859-1"));
                    outputStream = response.getOutputStream();
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = inputStream.read(buf)) > 0) {
                        outputStream.write(buf, 0, len);
                    }
                    response.flushBuffer();
                } catch (IOException e) {
//                    log.error("预览文件失败" + e.getMessage());
                    response.setStatus(404);
                    e.printStackTrace();
                } finally {
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e) {
//                            log.error(e.getMessage(), e);
                        }
                    }
                    if (outputStream != null) {
                        try {
                            outputStream.close();
                        } catch (IOException e) {
//                            log.error(e.getMessage(), e);
                        }
                    }
                }

            }
        }
    }

    public static void main(String[] args) {

    }


}


